// pages/independent/course_detail/course_detail.js
import CourseTool from "../../../utils/CourseTool.js"
import CommonTool from "../../../utils/CommonTool.js"
import NoteTool from "../../../utils/NoteTool.js"

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		courseDetail:{},
		tips:[
			"sprint",
			"sprint",
			"sprint"
		],
		signOfTabBar: 1,            //1课程目录 2课程评价 3课程笔记
		tabBar:[
			{
				"name":"课程目录",
				"sign":1
			},
			{
				"name":"课程评价",
				"sign":2
			},
			{
				"name":"课程笔记",
				"sign":3
			}
		],
		section_list:{},			// status 0未开始 1已完成 2学习中
		common_list:{},
		signOfFinish:[
			"_undo",
			"_done",
			"_doing"
		],
		content:[
			"未开始",
			"已完成",
			"学习中"
		],
		percent: 80,
		stars: 0,
		number:"",
		isShowInput:false,
		score:1,
		noteList:{}
	},

	setValue(e) {
		this.setData({
		  	number: e.detail.value
		})
	},
	cancel(){
		this.setData({
			isShowInput: false,
		})
	},
	confirmAcceptance(){
		this.setData({
			isShowInput: false,
		})
		let addCom = CommonTool.addCom(this.data.score,this.data.courseDetail.id,this.data.number);
		addCom.then((res)=>{
			console.log(res);
			if(res.code == 200){
				wx.showToast({
				  title: '评价成功',
				  icon: "success",
				  duration:3000
				})
			}
			else{
				console.log(1111111);
				wx.showToast({
					title: '您已评价',
					icon: "error",
					duration:3000
				})
			}
		})
	},
	realname(){
		this.setData({
			isShowInput: true
		})
	},

	slider2change(e){
		console.log(e.detail.value/20);
		this.setData({
			score: e.detail.value/20
		})
	},

	changeList(e) {
		let sign = e.currentTarget.dataset.sign;
		this.setData({
			signOfTabBar:sign
		})
		console.log(this.data.signOfTabBar);
		switch (this.data.signOfTabBar) {
			case 3:
				this.getCourseNote();
				break;
			case 2:
				this.getCourseCom();
				break;
			default:
				this.getSection();
				break;
		}
	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.setData({
			courseId: options.Id
		})
		this.setData({
			stars: options.stars+""
		})
		this.getCourseDetail();
		this.getSection();
		console.log(this.data.courseId);
	},

	goToCourseSection(e){
		console.log(e);
		let sectionInfo = e.currentTarget.dataset.sectioninfo;
		let str = JSON.stringify(sectionInfo);
		wx.navigateTo({
		  	url: '../course_section/course_section?sectionInfo=' + str,
		})
	},

	getSection(){
		let SectionTemp = CourseTool.getCourseSection(this.data.courseId);
		SectionTemp.then((res)=>{
			this.setData({
				section_list:res.data
			})
			console.log(this.data.section_list);

		})
	},

	getCourseDetail(){
		let detailTemp = CourseTool.getCourseDetail(this.data.courseId);
		detailTemp.then((res)=>{
			this.setData({
				courseDetail: res.data
			})
			console.log(this.data.courseDetail);
		})
	},

	getCourseCom(){
		let comList = CommonTool.getComList(this.data.courseId);
		comList.then((res)=>{
			console.log(111);
			console.log(res);
			for(let i=0;i<res.records.length;i++){
				res.records[i].createTime = res.records[i].createTime.slice(0,10);
			}
			this.setData({
				common_list: res
			})
		})
		// console.log(this.data.common_list.reccords[0].createTime.splice(0,10));
		// for(let i=0;i<this.data.common_list.records.length();i++){
		// 	this.setData({
		// 		['common_list.reccords[i].createTime']: this.data.common_list.reccords[i].createTime.splice(0,10)
		// 	})
		// }
	},

	getCourseNote(){
		let note_list = NoteTool.getCourseNote(this.data.courseId);
		note_list.then((res)=>{
			console.log(res);
			this.setData({
				noteList: res.data.records
			})
			console.log(this.data.noteList);
		})
	},

	openNote(e){
		let t = e.currentTarget.dataset.item;
		console.log(t);
		let str = JSON.stringify(t);
		wx.navigateTo({
		  	url: '/pages/independent/note_open/note_open?str='+str,
		})
	},

	joinCourse(){
		console.log(this.data.courseDetail);

		let join = CourseTool.joinCourse(this.data.courseDetail.id);
		join.then((res)=>{
			console.log(res);
			if(res.code == 200){
				wx.showToast({
				  title: '加入课程成功',
				  icon: "success",
				  duration:3000
				})
			}
			else{
				console.log(1111111);
				wx.showToast({
					title: '您已加入过',
					icon: "error",
					duration:3000
				})
			}
		})
	},

	addCom(){
		this.setData({
			isShowInput: true
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getCourseDetail();
		this.getSection();
		console.log(this.data.courseId);
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})